//
//  MPNavigationBar.m
//  MissPaparazzi
//
//  Created by Alexey Aleshkov on 30.08.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "MPNavigationBar.h"


#define LIGHT_COLOR_COMPONENTS      { (100.0 / 255.0), (100.0 / 255.0), (100.0 / 255.0), 1.0, (64.0 / 255.0), (64.0 / 255.0), (64.0 / 255.0), 1.0 }
#define MAIN_COLOR_COMPONENTS       { (64.0 / 255.0), (64.0 / 255.0), (64.0 / 255.0), 1.0, (28.0 / 255.0), (28.0 / 255.0), (28.0 / 255.0), 1.0 }

@implementation MPNavigationBar

- (void)dealloc
{
	[_shadowLayer removeFromSuperlayer];
	[_shadowLayer release];
	[super dealloc];
}

#pragma mark -
#pragma mark Overrides

- (void)awakeFromNib
{
	[super awakeFromNib];
	
	self.layer.masksToBounds = NO;
	
	CGRect navBarLayerBounds = self.layer.bounds;
	_shadowLayer = [[CAGradientLayer alloc] init];
	_shadowLayer.frame = CGRectMake(navBarLayerBounds.origin.x, navBarLayerBounds.origin.y + navBarLayerBounds.size.height, navBarLayerBounds.size.width, 6);
	_shadowLayer.colors = [NSArray arrayWithObjects:
						  (id)[UIColor colorWithWhite:0.2 alpha:0.75f].CGColor,
						  (id)[UIColor colorWithWhite:0.2 alpha:0.0f].CGColor,
						  nil];
	_shadowLayer.locations = [NSArray arrayWithObjects:
							 [NSNumber numberWithFloat:0.0f],
							 [NSNumber numberWithFloat:1.0f],
							 nil];
	[self.layer addSublayer:_shadowLayer];
}

- (void)drawRect:(CGRect)rect
{
	// emulate the tint colored bar
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGFloat locations[2] = { 0.0, 1.0 };
	CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
	
	CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
	CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
	CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
	CGGradientRelease(topGradient);
	
	CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
	CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
	CGContextDrawLinearGradient(context, botGradient,
								CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
	CGGradientRelease(botGradient);
	
	CGColorSpaceRelease(myColorspace);
	
	// top Line
	CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
	CGContextMoveToPoint(context, 0, 0);
	CGContextAddLineToPoint(context, self.frame.size.width, 0);
	CGContextStrokePath(context);
	
	// bottom line
	CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
	CGContextMoveToPoint(context, 0, self.frame.size.height);
	CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
	CGContextStrokePath(context);
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	CGRect navBarLayerBounds = self.layer.bounds;
	_shadowLayer.frame = CGRectMake(navBarLayerBounds.origin.x, navBarLayerBounds.origin.y + navBarLayerBounds.size.height, navBarLayerBounds.size.width, 6);
}

@end
